package com.zmh.service.mail;

import java.util.Date;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/** 
 * 邮件详解 - 标签 - YSOcean - 博客园
	http://www.cnblogs.com/ysocean/tag/%E9%82%AE%E4%BB%B6%E8%AF%A6%E8%A7%A3/
	http://you-java.iteye.com/blog/1453552
	邮箱被封：http://config.mail.163.com/settings/imap/index.jsp?uid=wwwxeb@163.com
 * 邮件发送类
 * @author zhoumh
 * @version 创建时间：2018年6月15日 下午5:10:40 
 * @Description 
 */
@Service
public class SendMailService {
	private static Logger logger = LoggerFactory.getLogger(SendMailService.class);

    //发件人地址
    private static String senderAddress = "zhoumh1985@qq.com";
    //收件人地址
    private static String recipientAddress = "tongzhi@zhoumh.top";
    //发件人账户名
    private static String senderAccount = "490850592@qq.com";
    //发件人账户密码
    private static String senderPassword = "jphciiaqzccscaid";
    
    private static Session session;
    
    public int sendMail(String subject, String content) {
        int result = 0;
        Properties props = new Properties();
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.username", senderAccount);
        props.put("mail.smtp.password", senderPassword);
        if(session == null)
            session = Session.getInstance(props);
//        session.setDebug(true);
        try
        {
            Message msg = getMimeMessage(session, subject, content);
            Transport transport = session.getTransport();
            transport.connect("smtp.qq.com", senderAccount, senderPassword);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        }
        catch(Exception e)
        {
            logger.error("\u90AE\u4EF6\u53D1\u9001\u5931\u8D25");
            e.printStackTrace();
        }
        return result;
    }
     
    /**
     * 获得创建一封邮件的实例对象
     * @param session
     * @return
     * @throws MessagingException
     * @throws AddressException
     */
    public static MimeMessage getMimeMessage(Session session,String subject, String content) throws Exception{
        //创建一封邮件的实例对象
        MimeMessage msg = new MimeMessage(session);
        //设置发件人地址
//        msg.setFrom(new InternetAddress(content.substring(0, content.indexOf(" "))+"<"+"senderAddress"+">"));
        msg.setFrom(new InternetAddress(content.substring(0, content.indexOf(" "))+"<"+senderAddress+">"));
        /**
         * 设置收件人地址（可以增加多个收件人、抄送、密送），即下面这一行代码书写多行
         * MimeMessage.RecipientType.TO:发送
         * MimeMessage.RecipientType.CC：抄送
         * MimeMessage.RecipientType.BCC：密送
         */
        msg.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(recipientAddress));
        //设置邮件主题
        msg.setSubject(subject,"UTF-8");
        //设置邮件正文
        msg.setContent(content, "text/html;charset=UTF-8");
        //设置邮件的发送时间,默认立即发送
        msg.setSentDate(new Date());
         
        return msg;
    }
 
}